<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>GEEK ROS WIFI</title>
    <style>
        * {
            box-sizing: border-box;
            -webkit-tap-highlight-color: transparent;
            -webkit-appearance: none;
            -webkit-touch-callout: none;
            outline: none;
            user-select: none;
            margin: 0;
            padding: 0;
            content: none;
        }
        body {
            font-family: -apple-system,BlinkMacSystemFont,"Helvetica Neue","PingFang SC","Hiragino Sans GB","Microsoft YaHei UI","Microsoft YaHei",Arial,sans-serif;
            font-feature-settings: "tnum";
            font-variant: tabular-nums;
            -webkit-font-smoothing: antialiased;
            font-size: 14px;
            content: none;
            color: #999999;
            background-color: #f9f9f9;
        }
        a {
            text-decoration: none;
            color: #999999;
        }
        .page-view{
            width: 100%;
            height: 100%;
            margin: 0 auto;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            z-index: 1;
            background-color: #000000;
        }
        .page-view .return-view{
            width: 35px;
            height: 35px;
            position: absolute;
            top: 10px;
            left: 10px;
            background-color: #000000;
            border-radius: 20px;
            text-align: center;
            padding-top: 10px;
        }
        .page-view .return-view img{
            width: 15px;
            height: 15px;
        }
        .page-view .header{
            width: 100%;
            height: 200px;
            text-align: center;
            padding: 50px 0;
            background-color: #1a1c1e;
        }
        .page-view .header .logo{
            width: 60px;
            height: 60px;
            margin: 0 auto;
        }
        .page-view .header .logo img{
            width: 60px;
            height: 60px;
        }
        .page-view .header .title{
            line-height: 40px;
            font-size: 14px;
        }
        .page-view .item-view{
            width: 100%;
            padding: 40px;
        }
        .page-view .item-view .wifi-view{
            width: 100%;
            display: none;
        }
        .page-view .item-view .wifi-view.show{
            display: block;
        }
        .page-view .item-view .wifi-view .item{
            width: 100%;
            height: 40px;
            margin-bottom: 10px;
        }
        .page-view .item-view .wifi-view .item:last-child{
            margin-top: 20px;
            text-align: center;
        }
        .page-view .item-view .wifi-view .item .item-select, .page-view .item-view .wifi-view .item .item-input{
            width: 100%;
            height: 40px;
            line-height: 40px;
            background-color: #1a1c1e;
            border: 0;
            padding: 0 10px;
            color: #999999;
            border-radius: 4px;
        }
        .page-view .item-view .wifi-view .item .item-select option:disabled{
            color: #333333;
        }
        .page-view .item-view .wifi-view .item .item-button{
            width: 200px;
            height: 40px;
            line-height: 40px;
            border-radius: 20px;
            background-color: #409EFF;
            border: 0;
            font-size: 12px;
            color: #f9f9f9;
        }
        .page-view .footer{
            width: 100%;
            height: 30px;
            position: fixed;
            left: 0;
            right: 0;
            bottom: 10px;
            font-size: 12px;
            text-align: center;
            line-height: 15px;
            color: #666666;
        }
        @media screen and (min-width: 1024px)
        {
            .page-view{
                max-width: 480px;
            }
        }
    </style>
</head>
<body onload="start_scan()">
    <div class="page-view">
        <div class="return-view">
            <a href="/">
                <img src="" />
            </a>
        </div>
        <div class="header">
            <div class="logo">
                <img src="" />
            </div>
            <div class="title">GEEK ROS ESP8266 WIFI</div>
        </div>
        <div class="item-view">
            <div id="wifi-view" class="wifi-view">
                <div class="item">
                    <select id="item-select" class="item-select">
                        <option value="" disabled selected>请选择要连接的WiFi</option>
                    </select>
                </div>
                <div class="item">
                    <input type="password" id="item-input" class="item-input" name="password" placeholder="请输入WiFi密码">
                </div>
                <div class="item">
                    <button id="item-button" class="item-button" onclick="onConnect()">连接到WiFi</button>
                </div>
            </div>
        </div>
        <div class="footer">
            <div class="title">Design From ESP8266-12F</div>
            <div class="title">www.geekros.com</div>
        </div>
    </div>
</body>
<script type="text/javascript">
    function start_scan(){
        let ajax = new XMLHttpRequest();
        ajax.open("get","/scan");
        ajax.send();
        ajax.onreadystatechange = function () {
            if(ajax.readyState === 4 && ajax.status === 200){
                let json_data = JSON.parse(ajax.responseText);
                let select = document.getElementById("item-select");
                if(json_data.data && json_data.data.length > 0){
                    json_data.data.forEach((tab, index) => {
                        if(tab !== null){
                            select.options.add(new Option(tab.ssid, tab.ssid));
                        }
                    });
                    document.getElementById("wifi-view").setAttribute("class", "wifi-view show");
                }else{
                    start_scan();
                }
            }
        };
    }
    function start_scan_connect(select, input){
        let button = document.getElementById("item-button");
        let ajax = new XMLHttpRequest();
        ajax.open("get","/scan/connect?select=" + select + "&input=" + input);
        ajax.send();
        ajax.onreadystatechange = function () {
            if(ajax.readyState === 4 && ajax.status === 200){
                let json_data = JSON.parse(ajax.responseText);
                if(json_data.code === 0){
                    button.innerHTML = "连接成功";
                }else{
                    button.innerHTML = "连接失败，重新尝试";
                }
            }
        }
    }
    function onConnect(){
        let select = document.getElementById("item-select");
        let input = document.getElementById("item-input");
        let button = document.getElementById("item-button");
        if(select.value !== ""){
            button.innerHTML = "正在连接";
            start_scan_connect(select.value, input.value);
        }
    }
    document.body.addEventListener('touchmove', function (e) {
        e.preventDefault();
    }, {passive: false});
</script>
</html>
